﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LowLevelGraphics.ColorSpaces;
using LowLevelGraphics.Histograms;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class FlatField : BaseImageFilter
    {
        protected UnsafeBitmap m_BackgroundImage = null;
        protected Statistics statistics = null;

        protected GaussianBlur m_GaussianBlur = new GaussianBlur();

        protected int m_nGaussianBlurIterations = 5;
        /// <summary>
        /// 
        /// </summary>
        public FlatField()
        {
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <param name="_flatField"></param>
        internal FlatField(FlatField _flatField)
        {
            m_nGaussianBlurIterations = _flatField.m_nGaussianBlurIterations;
            m_BackgroundImage = _flatField.m_BackgroundImage;
        }

        /// <summary>
        /// Gets or sets Gaussian Blur Iterations
        /// </summary>
        public int GaussianBlurIterations
        {
            get { return m_nGaussianBlurIterations; }
            set { m_nGaussianBlurIterations = value; }
        }

        /// <summary>
        /// Gets or sets background Image
        /// </summary>
        public UnsafeBitmap BackgroundImage
        {
            get { return m_BackgroundImage; }
            set { m_BackgroundImage= value; }
        }

        /// <summary>
        /// Executes this filter...
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            statistics = new Statistics(_bitmap);
            for (int i = 0; i < m_nGaussianBlurIterations; i++)
            {
                m_GaussianBlur.Execute(m_BackgroundImage);
            }

            double dMeanR = statistics.MeanR;
            double dMeanG = statistics.MeanG;
            double dMeanB = statistics.MeanB;

            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;

            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    // red
                    if (m_BackgroundImage[x,y].R != 0)
                    {
                        _bitmap[x,y,RGBChannel.R] = (byte)Math.Min(dMeanR * _bitmap[x, y].R / m_BackgroundImage[x,y].R, 255);
                    }
                    // green
                    if (m_BackgroundImage[x,y].G != 0)
                    {
                        _bitmap[x,y,RGBChannel.G] = (byte)Math.Min(dMeanG * _bitmap[x, y].G / m_BackgroundImage[x,y].G, 255);
                    }
                    // blue
                    if (m_BackgroundImage[x,y].B != 0)
                    {
                        _bitmap[x,y, RGBChannel.B] = (byte)Math.Min(dMeanB * _bitmap[x, y].B / m_BackgroundImage[x,y].B, 255);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new FlatField(this);
        }
    }
}
